package com.leo.jdkcore.guava;

import org.junit.Test;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;

public class GuavaTest {
	
	@Test
	public void testSplitter(){
		String s = "2,,4,    3,,,1";
		Iterable<String> ites = Splitter.on(',').split(s);
		for (String ite : ites) {
			System.out.println(ite);
		}
	}
	/**
	 * 忽略空字符串
	 */
	@Test
	public void testSplitterIgnoreEmptyStr(){
		String s = "2,,4,    3,,,1";
		Iterable<String> ites = Splitter.on(',').omitEmptyStrings().split(s);
		for (String ite : ites) {
			System.out.println(ite);
		}
	}
	/**
	 * 分割字符串，取出头尾空格
	 */
	@Test
	public void testSplitterTrimStr(){
		String s = "2,,4,    3,,,1";
		Iterable<String> ites = Splitter.on(',').trimResults().omitEmptyStrings().split(s);
		for (String ite : ites) {
			System.out.println(ite);
		}
		
	}
	
	/**
	 * 限制分割的次数
	 */
	@Test
	public void testSplitterTrimStrLimit(){
		String s = "2,,4,    3,,,1";
		Iterable<String> ites = Splitter.on(',').trimResults().omitEmptyStrings().limit(3).split(s);
		for (String ite : ites) {
			System.out.println(ite);
		}
		
	}
	
	/**
	 * Trim more characters
	 */
	@Test
	public void testSplitterTrimChar(){
		String s = "x,y,h4,  j,,e";
		Iterable<String> ites = Splitter.on(',').trimResults(CharMatcher.DIGIT).omitEmptyStrings().split(s);
		for (String ite : ites) {
			System.out.println(ite);
		}
	}
	
}
